<form class="layui-form seller-form" action="">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">商品名称：</label>
            <div class="layui-input-inline seller-inline-3">
                <input type="text"  name="name" placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-sm" lay-submit="" lay-filter="goods-search"><i class="iconfont icon-chaxun"></i>筛选</button>
            </div>
        </div>
    </div>
</form>

<div class="table-body">
    <table id="stockTable" lay-filter="stockTable"> </table>
</div>
<script type="text/html" id="btn">
    <a class="layui-btn layui-btn-xs edit-class" lay-event="rk" >入库</a>
    <a class="layui-btn layui-btn-xs edit-class" lay-event="ck" >出库</a>
</script>
<script type="text/html" id="form">
    <form class="layui-form" action="" style="padding: 30px;">
    <input type="hidden" name="id" value="{{ d.id }}" />
    <input type="hidden" name="type" value="{{ d.type }}" />
    <div class="layui-form-item">
        <label class="layui-form-label">数量</label>
        <div class="layui-input-block">
          <input type="text" name="num" autocomplete="off" placeholder="请输入数量" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
          <textarea placeholder="请输入内容" name="meno" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
          <button class="layui-btn" lay-submit lay-filter="save">立即提交</button>
           </div>
        </div>
    </form>
</script>
<script>
    layui.use(['laydate', 'table', 'layer', 'form', 'laytpl'], function () {
        var laydate = layui.laydate;
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        var laytpl = layui.laytpl;
        var lay_index;
        var run = table.render({
            elem: '#stockTable',
            url: "{:url('stock')}?_ajax=1",
            cols: [[
                { field: 'id', minWidth: 120, title: 'ID', sort: true, align: 'center' },
                { field: 'goods_id', minWidth: 120, title: '商品ID', align: 'center' },
                { field: 'goods_name', minWidth: 150, title: '商品名称', align: 'center'},
                { field: 'stock', minWidth: 150, title: '库存', align: 'center' },
                {field:'operating',width:120,title:'操作', align: 'center', toolbar: '#btn', fixed: 'right'}
            ]],
            page:true
        });
        form.on('submit(save)', function(data){
            layer.load(0, {shade: false});
            $.ajax({
                url: "{:url('edit')}",
                type: 'POST',
                data: data.field,
                dataType: 'json',
                success: function(respon) {
                    layer.msg('编辑成功');
                    layer.closeAll();
                    run.reload();
                },
                error: function (respon) {
                    layer.msg(respon.responseJSON.msg, {icon: 5});
                }
            })
            return false;
        });
        form.on('submit(goods-search)', function(data){
            var tempfilter=data.field//合并tab筛选和普通搜索
            run.reload({
                where: tempfilter
                ,page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        table.on('tool(stockTable)', function(obj){
            var data = obj.data;
            layer.prompt({
			  formType: 1,
			  title: '请输入密码',
			},function(val, index){
                $.ajax({
                    url: "{:url('user/balancePassword')}",
                    type: 'POST',
                    data: {password: val},
                    dataType: 'json',
                    success: function(respon) {
                        //open(data)
                        if(obj.event == 'rk') {
                            var title = '商品: '+data.goods_name +'入库';
                            open(title, data.id, 'rk')
                        } else if(obj.event == 'ck') {
                            var title = '商品: '+data.goods_name +'出库';
                            open(title, data.id, 'ck')
                        }
                        layer.close(index);
                    },
                    error: function (respon) {
                        layer.msg(respon.responseJSON.msg, {icon: 5});
                    }
                })
                
            });
            /*if(obj.event == 'rk') {
                var title = '商品: '+data.goods_name +'入库';
                open(title, data.id, 'rk')
            } else if(obj.event == 'ck') {
                var title = '商品: '+data.goods_name +'出库';
                open(title, data.id, 'ck')
            }*/
        });
        var open = function (title, id, type) {
            var form = $('#form').html();
            laytpl(form).render({id: id, type: type}, function(html){
                lay_index = layer.open({
                    type: 1,
                    title: title,
                    shadeClose: true,
                    shade: false,
                    area: ['480px', '350px'], //宽高
                    content: html
                  });
            });
            
        }
    });
</script>